﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RBAC.Api.Erroc;
using RBAC.Api.Writh.Command.Permissions;
using RBAC.Api.Writh.Command.Roles;
using RBAC.Api.Writh.Command.Users;
using System.Diagnostics;

namespace RBAC.Api.Writh.Controllers
{
    /// <summary>
    /// 管理员控制器--写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagerController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ManagerController> logger;

        public ManagerController(IMediator mediator, ILogger<ManagerController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddPermission(AddPermissionCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理添加权限请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("添加权限请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加权限请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"添加权限失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("添加权限请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdatePermission(UpdatePermissionCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理修改权限请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("修改权限请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改权限请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"修改权限失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("修改权限请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }

        /// <summary>
        /// 删除权限（逻辑删除）
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DeletePermission(DeletePermissionCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理删除权限请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("删除权限请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除权限请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"删除权限失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("删除权限请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        
        /// <summary>
        /// 修改权限状态
        /// </summary>
        /// <param name="command">包含权限ID和状态值的命令对象</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdatePermissionState(UpdatePermissionStateCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理修改权限状态请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("修改权限状态请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改权限状态请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"修改权限状态失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("修改权限状态请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }

        /// <summary>
        /// 添加角色权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddRolePermission(AddRoleCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理添加角色权限请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("添加角色权限请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加角色权限请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"添加角色权限失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("添加角色权限请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        /// <summary>
        /// 修改角色权限
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdateRolePermission(UpdateRoleCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理修改角色权限请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("修改角色权限请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改角色权限请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"修改角色权限失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("修改角色权限请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        /// <summary>
        /// 删除角色权限(逻辑删除)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DeleteRolePermission(DeleteRoleCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理删除角色权限请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("删除角色权限请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除角色权限请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"删除角色权限失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("删除角色权限请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        
        /// <summary>
        /// 修改角色状态
        /// </summary>
        /// <param name="command">包含角色ID和状态值的命令对象</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdateRoleState(UpdateRoleStateCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理修改角色状态请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("修改角色状态请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改角色状态请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"修改角色状态失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("修改角色状态请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddUserRole(AddUserCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理添加用户角色请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("添加用户角色请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加用户角色请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"添加用户角色失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("添加用户角色请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        /// <summary>
        /// 修改用户角色
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdateUserRole(UpdateUserCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理修改用户角色请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("修改用户角色请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改用户角色请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"修改用户角色失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("修改用户角色请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        /// <summary>
        /// 删除用户角色(逻辑删除)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DeleteUserRole(DeleteUserCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理删除用户角色请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("删除用户角色请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除用户角色请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"删除用户角色失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("删除用户角色请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
        
        /// <summary>
        /// 修改用户状态
        /// </summary>
        /// <param name="command">包含用户ID和状态值的命令对象</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdateUserState(UpdateUserStateCommand command)
        {
            var stopwatch = Stopwatch.StartNew();
            logger.LogInformation("开始处理修改用户状态请求");
            
            try
            {
                var result = await mediator.Send(command);
                logger.LogInformation("修改用户状态请求处理成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改用户状态请求处理异常: {Message}", ex.Message);
                return new APIResult<object> { Code = ResultCode.Fail, Message = $"修改用户状态失败: {ex.Message}" };
            }
            finally
            {
                stopwatch.Stop();
                logger.LogInformation("修改用户状态请求处理完成，耗时: {ElapsedMilliseconds}ms", stopwatch.ElapsedMilliseconds);
            }
        }
    }
}